﻿using FreeSql;
using System.Linq.Expressions;

namespace Msung.Core.Freesql
{
    public abstract class BaseRepositoryCore<TEntity, L> : BaseRepository<TEntity, L> where TEntity : class
    {
        protected IAdo Ado;
        public BaseRepositoryCore(IFreeSql fsql,
            Expression<Func<TEntity, bool>> filter,
            Func<string, string> asTable = null) : base(fsql, filter, asTable)
        {
            Ado = fsql.Ado;
        }
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int ExcuteSql(string sql)
        {
            int count = Ado.ExecuteNonQuery(sql);
            return count;
        }
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<int> ExcuteSqlAsync(string sql)
        {
            int count = await Ado.ExecuteNonQueryAsync(sql);
            return count;
        }
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int ExcuteSql(TEntity entity)
        {
            string sql = Orm.InsertOrUpdate<TEntity>().SetSource(entity).ToSql();
            int count = Ado.ExecuteNonQuery(sql);
            return count;
        }
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<int> ExcuteSqlAsync(TEntity entity)
        {
            string sql = Orm.InsertOrUpdate<TEntity>().SetSource(entity).ToSql();
            int count = await Ado.ExecuteNonQueryAsync(sql);
            return count;
        }
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int ExcuteSql(List<TEntity> entities)
        {
            string sql = Orm.InsertOrUpdate<TEntity>().SetSource(entities).ToSql();
            int count = Ado.ExecuteNonQuery(sql);
            return count;
        }
        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public async Task<int> ExcuteSqlAsync(List<TEntity> entities)
        {
            string sql = Orm.InsertOrUpdate<TEntity>().SetSource(entities).ToSql();
            int count = await Ado.ExecuteNonQueryAsync(sql);
            return count;
        }
    }

}
